package com.cow.service.impl;

import com.cow.config.AIModelClient;
import com.google.common.util.concurrent.RateLimiter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.regex.Pattern;

/**
 *
 */
@Service
public class AIService {

    @Autowired
    private AIModelClient aiModelClient;

    private final RateLimiter rateLimiter = RateLimiter.create(5); // 每秒5次请求

    // 带简单上下文的对话处理
    public String processQuestion(String question, String sessionId) {
        // 限流检查
        if (!rateLimiter.tryAcquire()) {
            throw new RuntimeException("请求过于频繁，请稍后再试");
        }

        // 敏感词过滤
        if (containsSensitiveWords(question)) {
            return "您的问题包含不合适内容，请重新提问";
        }

        // 调用AI模型（示例使用阿里云DashScope）
        return aiModelClient.queryAI(question);
    }

    private boolean containsSensitiveWords(String text) {
        // 实现敏感词过滤逻辑（示例正则）
        Pattern pattern = Pattern.compile("(敏感词1|敏感词2)", Pattern.CASE_INSENSITIVE);
        return pattern.matcher(text).find();
    }
}
